//
//  MineCollectViewController.m
//  Business Club
//
//  Created by 尹宁 on 16/4/15.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MineCollectViewController.h"
#import "MineEventModel.h"
//#import "FindActivityCell.h"
//#import "FindModel.h"
#import "UIViewController+HUD.h"


//#import "FindDetailViewController.h"
@interface MineCollectViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSString *token;
    NSString *page;
    UIView *top;
    UIView *bottom;
    
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) UIView *thingView;
@property (nonatomic, strong) UIButton *haveThingButton;
@property (nonatomic, strong) UIButton *helpThingButton;
@property (nonatomic, strong) UIButton *eventButton;
@property (nonatomic, strong) UIView *haveThingView;

@property (nonatomic, strong) NSString *url;    //当前的url
@property (nonatomic, strong) NSString *state;
@end

@implementation MineCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我的收藏";
    [self creatSegment];
    [self initTableView];
    token = [NSUSERDEFAULTS objectForKey:@"gettoken"];
    _state = @"活动";
    
    [self listNetHandle:@"/My/myCollectHd.html"];
}

- (void)creatSegment
{
    if (_thingView == nil) {
        
        _thingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 40)];
        _thingView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_thingView];
        
        _eventButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _eventButton.frame = CGRectMake(10 * KSCREENWIDTH / 375, 5, KSCREENWIDTH / 5, 30);
        [_eventButton setTitle:@"活动" forState:UIControlStateNormal];
        [_thingView addSubview:_eventButton];
        
        _haveThingButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _haveThingButton.frame = CGRectMake(140 * KSCREENWIDTH / 375, 5, KSCREENWIDTH / 5, 30);
        [_haveThingButton setTitle:@"我有事" forState:UIControlStateNormal];
        [_thingView addSubview:_haveThingButton];
        
        _helpThingButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _helpThingButton.frame = CGRectMake(280 * KSCREENWIDTH / 375, 5, KSCREENWIDTH / 5, 30);
        [_helpThingButton setTitle:@"我帮事" forState:UIControlStateNormal];
        [_thingView addSubview:_helpThingButton];
        
        [_eventButton addTarget:self action:@selector(event) forControlEvents:UIControlEventTouchUpInside];
        [_haveThingButton addTarget:self action:@selector(haveThing) forControlEvents:UIControlEventTouchUpInside];
        [_helpThingButton addTarget:self action:@selector(helpThing) forControlEvents:UIControlEventTouchUpInside];
        
        
        // 上下方的2条线
        top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 0.5)];
        top.backgroundColor = [UIColor lightGrayColor];
        top.alpha = 0.3;
        [_thingView addSubview:top];
        
        bottom = [[UIView alloc] initWithFrame:CGRectMake(0, _thingView.bounds.size.height - 1, KSCREENWIDTH, 0.5)];
        bottom.backgroundColor = [UIColor lightGrayColor];
        bottom.alpha = 0.3;
        [_thingView addSubview:bottom];
        
        // 下方横条
        _haveThingView = [[UIView alloc]initWithFrame:CGRectMake(_eventButton.frame.origin.x + 10, _thingView.bounds.size.height - 3, _haveThingButton.bounds.size.width - 20, 1)];
        _haveThingView.backgroundColor = [UIColor redColor];
        [_thingView addSubview:_haveThingView];
        
        _eventButton.tintColor = [UIColor redColor];
        _haveThingButton.tintColor = [UIColor blackColor];
        _helpThingButton.tintColor = [UIColor blackColor];
        _eventButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _haveThingButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _helpThingButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
    }
    
}

- (void)initTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _thingView.bounds.size.height, KSCREENWIDTH, KSCREENHEIGHT - _thingView.bounds.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.rowHeight = KSCREENHEIGHT / 6.3;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];// 如果没有数据下方不显示分割线
    [_tableView registerNib:[UINib nibWithNibName:@"FindActivityCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

//页面数据的网络请求 传进来的是哪个地址就请求哪个地址
- (void)listNetHandle:(NSString *)url
{
    _url = url;
    self.listArr = [NSMutableArray array];
    YTKNetHandle *api1 = [[YTKNetHandle alloc]initWithUrl:[NSString stringWithFormat:@"%@?token=%@",url,token] RegisterApiMethod:RequestMethodGet];
    [api1 startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        id result = request.responseBody;
        NSLog(@"%@", result);
        if (![result[@"error"] isEqualToString:@"0"]) {
            NSLog(@"%@",result[@"msg"]);
        }else{
            NSDictionary *dic = result[@"result"];
            NSArray *arr = dic[@"data"];
            for (NSDictionary *dicc in arr) {
//                FindModel *mine = [[FindModel alloc]init];
//                [mine setValuesForKeysWithDictionary:dicc];
//                [self.listArr addObject:mine];
            }
            if (arr.count == 0) {
                UIImageView *view = [[UIImageView alloc]initWithFrame:_tableView.frame];
                view.image = [UIImage imageNamed:@"缺省页"];
                _tableView.backgroundView = view;
            }else{
                
                    UIView *view = [[UIView alloc]initWithFrame:_tableView.frame];
                    view.backgroundColor = [UIColor whiteColor];
                    _tableView.backgroundView = view;
            }
            [_tableView reloadData];
            
        }
        
        
    } failure:^(YTKBaseRequest *request) {
        
    }];
    
    
}


//活动触发方法
- (void)event
{
    _state = @"活动";

    _eventButton.tintColor = [UIColor redColor];
    _haveThingButton.tintColor = [UIColor blackColor];
    _helpThingButton.tintColor = [UIColor blackColor];
//    [self listNetHandle:@"/My/myCollectHd.html"];
    [self setupRefresh];
    [UIView animateWithDuration:0.15 animations:^{
        _haveThingView.frame = CGRectMake(_eventButton.frame.origin.x + 10, _thingView.bounds.size.height - 3, _haveThingButton.bounds.size.width - 20, 1);
    }];
}

// 我有事触发方法
- (void)haveThing
{
    _state = @"我有事";

    _eventButton.tintColor = [UIColor blackColor];
    _haveThingButton.tintColor = [UIColor redColor];
    _helpThingButton.tintColor = [UIColor blackColor];
//    [self listNetHandle:@"/My/myCollectWys.html"];
    [self setupRefresh];
    [UIView animateWithDuration:0.15 animations:^{
        _haveThingView.frame = CGRectMake(_haveThingButton.frame.origin.x + 10, _thingView.bounds.size.height - 3, _haveThingButton.bounds.size.width - 20, 1);
    }];
}
// 我帮事触发方法
- (void)helpThing
{
    _state = @"我帮事";

    _eventButton.tintColor = [UIColor blackColor];
    _haveThingButton.tintColor = [UIColor blackColor];
    _helpThingButton.tintColor = [UIColor redColor];
//    [self listNetHandle:@"/My/myCollectWbs.html"];
    [self setupRefresh];
    [UIView animateWithDuration:0.15 animations:^{
        _haveThingView.frame = CGRectMake(_helpThingButton.frame.origin.x + 10, _thingView.bounds.size.height - 3, _haveThingButton.bounds.size.width - 20, 1);
    }];
}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    FindActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (self.listArr.count != 0) {
//        FindModel *find = self.listArr[indexPath.row];
//        cell.find = find;
//    }
//    cell.isHotLabel.hidden = YES;
//    cell.deleteButton.hidden = YES;
//    
//    return cell;
//    
//}
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    FindDetailViewController *findDetailVC = [[FindDetailViewController alloc]init];
//    findDetailVC.find = _listArr[indexPath.row];
//    if (_eventButton.tintColor == [UIColor redColor]) {
//        findDetailVC.state = @"活动";
//    } else if (_haveThingButton.tintColor == [UIColor redColor]) {
//        findDetailVC.state = @"我有事";
//    } else {
//        findDetailVC.state = @"我帮事";
//    }
//    [self.navigationController pushViewController:findDetailVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}


- (void)setupRefresh
{
    //下拉刷新
    //    [self headerRereshing];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
}
//下拉刷新
- (void)headerRereshing
{
    [self.listArr removeAllObjects];
    page = @"1";
    if ([_state isEqualToString:@"活动"]) {
        
        [self listNetHandle:@"/My/myCollectHd.html"];
    }else if ([_state isEqualToString:@"我有事"]){
        
        [self listNetHandle:@"/My/myCollectWys.html" ];
    }else{
        
        [self listNetHandle:@"/My/myCollectWbs.html"];
        
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //2.结束刷新
        [_tableView reloadData];
        [self.tableView headerEndRefreshing];
    });
}
//上拉加载
- (void)footerRereshing
{
    
    if ([_state isEqualToString:@"活动"]) {
        
        [self listNetHandle:@"/My/myCollectHd.html"];
    }else if ([_state isEqualToString:@"我有事"]){
        [self listNetHandle:@"/My/myCollectWys.html"];
        
    }else{
        [self listNetHandle:@"/My/myCollectWbs.html"];
        
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //2.结束刷新
        [_tableView reloadData];
        
        [self.tableView footerEndRefreshing];
    });
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
//    [self setupRefresh];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
